home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1007 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.5 KB  |  93 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Problems with inheritance and templates in g++
  5. Date: 08 Jan 1996 18:32:06 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan8193206@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4cre8f$bkp@magus.cs.utah.edu>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: bendixen@eng.utah.edu's message of 8 Jan 1996 15:46:23 GMT
  12.  
  13. In article <4cre8f$bkp@magus.cs.utah.edu> bendixen@eng.utah.edu (Mason Bendixen) writes:
  14.  
  15.    Hi,
  16.     During the link stage, g++ complains about
  17.    undefined symbols that correspond to unused member
  18.    functions of instantiated objects. The code in question
  19.    compiles fine under Borland C++ 4.5 but not g++.
  20.    Take the sample file:
  21.  
  22.    // BEGIN SAMPLE PROGRAM
  23.  
  24.    #include <iostream.h>
  25.  
  26.    template <class T>
  27.    class base {
  28.    public:
  29.        base () {}
  30.        virtual int a() = 0;
  31.        virtual int b() = 0;
  32.        static base<T>* make();
  33.    };
  34.  
  35.    template <class T>
  36.    class test : public base<T> {
  37.    public:
  38.        test() {}
  39.        int a();
  40.        int b();
  41.    };
  42.  
  43.    template <class T>
  44.    base<T>* base<T>::make() {return new test<T>;}
  45.  
  46.    template <class T>
  47.    int test<T>::a() {return 1;}
  48.  
  49.    template <class T>
  50.    int test<T>::b() {return 2;}
  51.  
  52.    int main() {
  53.        test<int> b;
  54.        //int i = b.a();
  55.        //int j = b.b();
  56.        base<int>* t = base<int>::make();
  57.        cout << t->a() << endl;
  58.        return 0;
  59.    }
  60.  
  61.    // END SAMPLE PROGRAM
  62.  
  63.  
  64.    When I try to compile I get:
  65.  
  66.    >> 97 cadesm48% g++ -o tester temp.C
  67.    >> collect2: ld returned 2 exit status
  68.    >> ld: Undefined symbol
  69.    >>    test<int>::a(void)
  70.    >>    test<int>::b(void)
  71.  
  72.    If I uncomment the following lines:
  73.  
  74.    >>      int i = b.a();
  75.    >>      int j = b.b();
  76.  
  77.    The program will work just fine.
  78.    Can I force g++ to instantiate all member of every
  79.    instantiated object? I think this has something to
  80.    do with the way g++ handles inheritance for templatized
  81.    classes. Any suggestions, insight, or possible work
  82.    a rounds in this age of pre- standardized C++ would be
  83.    greatly appreciated.
  84.  
  85. I think you are using an obsolete version of g++.
  86. Anyway you can force the implementation of any member-function of a generic
  87. class by using explicit template instantiation. For example to instantiate
  88. test<int> and all its member-functions use
  89.  
  90.                         template class test<int>;
  91.  
  92.         Enno
  93.